'''
Created on Apr 20, 2012

@author: root
'''

from Base import Base
from NodeInfo import NodeInfo
from User import User


#import threading
#import datetime
import os
#import rpm
import re
#import time

class PreCheck(Base):
   
    Nodes = []
    Users = []
    projectName = ""
    
    def CheckBinariesPath(self,gphdPath):
        print "    * Verifying Binaries Tarball "+str(gphdPath)   
        self.LogInfo("Verifying Binaries Tarball "+str(gphdPath))  
        fileTest = os.popen("file -sL " + str(gphdPath)).read()
        if ("gzip compressed data" in str(fileTest)):
            output =  str(gphdPath)+"  Verified as a Valid Tarball"
            self.LogInfo(output)
            print "        * "+output
            return 0
        else:
            output =  "File specified as Tarball is not a GZip Compressed Tarball"
            self.LogError(output)
            print "        * ERROR: " + str(output)
            return -1
        
    def CheckConfigPath(self,configPath):
      
        print "    * Verifying Path to Cluster Configuration File "+str(configPath)   
        self.LogInfo("Verifying Path to Cluster Configuration File "+str(configPath))  
        
        if  os.path.isfile(configPath):
            output =  "Configuration File Found"
            self.LogInfo(output)
            print "        * "+ output
            return 0
        else:
            output =  "Configuration File Not Found"
            self.LogError(output)
            print "        * ERROR: " + str(output)
            return -1    

    def CheckJavaPath(self,javaPath):
        print "    * Verifying Path to Java RPM File "+str(javaPath)   
        self.LogInfo("Verifying Path to Java RPM File "+str(javaPath))  
        
        if  os.path.isfile(javaPath):
            output =  "Java RPM File Found"
            self.LogInfo(output)
            print "        * "+output
            return 0
        else:
            output =  "Java RPM File Not Found"
            self.LogError(output)
            print "        * ERROR: " + str(output)
            return -1    
                     

    def CheckPing(self,Nodes):
        pingStatus = 0
#        print "---------------------------------------------------"
#        print "       Network Verification Tests"
#        print "---------------------------------------------------"
#        print "    * Verifying Ping Connectivity to Cluster Nodes"  
        self.LogInfo("Verifying Ping Connectivity to Cluster Nodes")
        for node in Nodes:
            ping = "".join(os.popen("ping -c3 "+node.hostName).readlines())
            if re.search("64 bytes from",ping):
            #    print "        * Node: "+node.hostName+" Responds to Ping"
                self.LogInfo("Node: "+node.hostName+" Responds to Ping")
            else:
            #    print "        * Node: "+node.hostName+" Did Not Respond"
                self.LogError("Node: "+node.hostName+" Did Not Respond")
                pingStatus = -1
        if (pingStatus ==0):
            self.LogInfo("Ping Test Successful")
            return 0
        else:
            self.LogError("Ping Test Failed")
            print "    * ERROR:  Failed to Contact all Cluster hosts"
            return -1
            





    def __init__(self):
        Base.__init__(self)
        print "---------------------------------------------------"
        print "        Running Pre-installatition Verification Tests"
        print "---------------------------------------------------"

                